/*
 * @(#)JMXUtil.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.util;

import java.lang.management.ManagementFactory;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import com.nhncorp.usf.core.jmx.JVMMBean;
import com.nhncorp.usf.core.jmx.WebModuleMBean;

/**
 * @author Web Platform Development Team
 */
public class JMXUtil {
    
    /**
     * Register m bean.
     *
     * @param jvmMBean the jvm m bean
     * @throws Exception the exception
     */
    public static void registerMBean(JVMMBean jvmMBean) throws Exception {
        register(jvmMBean, jvmObjectName(jvmMBean));
    }

    /**
     * Register m bean.
     *
     * @param webModuleMBean the web module m bean
     * @throws Exception the exception
     */
    public static void registerMBean(WebModuleMBean webModuleMBean) throws Exception {
        register(webModuleMBean, webModuleObjectName(webModuleMBean));
    }

    /**
     * Unregister m bean.
     *
     * @param jvmMBean the jvm m bean
     * @throws Exception the exception
     */
    public static void unregisterMBean(JVMMBean jvmMBean) throws Exception {
        unregister(jvmObjectName(jvmMBean));
    }

    /**
     * Unregister m bean.
     *
     * @param webModuleMBean the web module m bean
     * @throws Exception the exception
     */
    public static void unregisterMBean(WebModuleMBean webModuleMBean) throws Exception {
        unregister(webModuleObjectName(webModuleMBean));
    }

    /**
     * Register.
     *
     * @param object     the object
     * @param objectName the object name
     * @throws Exception the exception
     */
    private static void register(Object object, ObjectName objectName) throws Exception {
        ManagementFactory.getPlatformMBeanServer().registerMBean(object, objectName);
    }

    /**
     * Unregister.
     *
     * @param objectName the object name
     * @throws Exception the exception
     */
    private static void unregister(ObjectName objectName) throws Exception {
        ManagementFactory.getPlatformMBeanServer().unregisterMBean(objectName);
    }

    /**
     * Jvm object name.
     *
     * @param jvm the jvm
     * @return the object name
     * @throws MalformedObjectNameException the malformed object name exception
     */
    private static ObjectName jvmObjectName(JVMMBean jvm) throws MalformedObjectNameException {
        return createObjectName(jvm.getName(), JVMMBean.TYPE);
    }

    /**
     * Web module object name.
     *
     * @param webModuleMBean the web module m bean
     * @return the object name
     * @throws MalformedObjectNameException the malformed object name exception
     */
    private static ObjectName webModuleObjectName(WebModuleMBean webModuleMBean) throws MalformedObjectNameException {
        return createObjectName(webModuleMBean.getName(), WebModuleMBean.TYPE);
    }

    /**
     * Creates the object name.
     *
     * @param name the name
     * @param type the type
     * @return the object name
     * @throws MalformedObjectNameException the malformed object name exception
     */
    private static ObjectName createObjectName(String name, String type) throws MalformedObjectNameException {
        return new ObjectName("USF:name=" + name + ",type=" + type);
    }
}
